// pages/device/index.js
import {handlerGobackClick, hint} from "../../../utils/util";
import {Device} from "../../../models/device";

const device = new Device()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:[true,false],
    tabOne:[],
    loading:false,
    noMore:false,
    pageNo:1,
    loadingFailed:false,

    tabOne0:[],
    loading0:false,
    noMore0:false,
    pageNo0:1,
    loadingFailed0:false,

    //弹出层
    show:false,
    isAdd:false,
    newDevice:{},
    showDevice:false,
    //筛选层
    selectedIndex:undefined
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initDataPage()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  handlerGobackClick,
  changeItme(event){
    var index=event.currentTarget.dataset['index'];
    var active = [false,false]
    active[index] = true
    this.setData({
      active,
      activeTab1:0
    })
  },
  goRentDetail(e){
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url:'/owner-app/pages/device-detail/index?id='+id
    })
  },
  addDevice(){
    wx.scanCode({
      success: (res) => {
        if(res.errMsg === 'scanCode:ok'){
          hint('扫描成功')
          let result = res.result
          device.getType(result)
              .then((res)=>{
                this.setData({
                  show:true,
                  newDevice:res.result
                })
              })
              .catch((err)=>{
                console.log(err);})

        }else {
          hint('扫码失败')
        }
      }
    })
  },
  initDataPage(){
    device.deviceList()
        .then((res)=>{
          this.setData({
            tabOne:res.result
          })
        })
        .catch((err)=>{
          console.log(err);})
    this.getData('alarmList','loading0','tabOne0','pageNo0','loadingFailed0',undefined,'noMore0',true)
  },
  getData(methodName,loadingName,dataName,pageNoName,loadingFailedName,id ,noMoreName,isPage){
    device[methodName](this.data[pageNoName],10,id)
        .then((res)=>{
          this.setData({
            [loadingName]: false
          })
          //有分页
          if(isPage){
            this.setData({
              [dataName]:this.data[dataName].concat(res.result.records)
            })
          }
          //没有分页就直接赋值
          else {
            this.setData({
              [dataName]:res.result.records
            })
          }
          if(res.result.records.length== 0){
            this.setData({
              [noMoreName]: true
            })
          }

        })
        .catch((err)=>{
          this.setData({
            [loadingFailedName]: true
          })
        })
  },
  scrollBottom(){
    if (!this.data.loading0 && !this.data.noMore0){
      this.setData({
        loading0: true,
        pageNo0: this.data.pageNo0 + 1
      })
      this.getData('alarmList','loading0','tabOne0','pageNo0','loadingFailed0',undefined,'noMore0',true)
    }
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  done(){
    this.setData({
      show:false
    })
    this.initDataPage()
  },
  addDeviceOk(){
    device.add(this.data.newDevice.id)
        .then((res)=>{
          hint(res.message)
          this.setData({
            isAdd:true
          })
        })
        .catch((err)=>{
          console.log(err);})
  },
  onCloseSelect(){
    this.setData({ showDevice: false });
  },
  showWrapper(){
    this.setData({ showDevice: true });
  },
  selectedItem(event){
    var index=event.currentTarget.dataset['index'];
    if(this.data.selectedIndex === index){
      console.log('相等');
      index = -1
    }
    this.setData({
      selectedIndex:index,
      showDevice: false
    })
    let id
    if(index>=0){
      id = this.data.tabOne.typeList[this.data.selectedIndex].id
    }else {
      id = undefined
    }

    //请求对应类型的数据
    device.deviceList(id)
        .then((res)=>{
          this.setData({
            tabOne:res.result
          })
        })
        .catch((err)=>{
          console.log(err);})
  },
})
